package com.jsz.yun.ai.field.controller;

import com.jsz.yun.ai.field.common.Req;
import com.jsz.yun.ai.field.common.Rsp;
import com.jsz.yun.ai.field.dto.UserInfoDTO;
import com.jsz.yun.ai.field.entity.UserEntity;
import com.jsz.yun.ai.field.service.UserInfoService;
import com.jsz.yun.ai.field.util.ThreadLocalSetting;
import com.jsz.yun.ai.field.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author gaoxing
 * @date 2025-03-24 18:03:50
 */
@Slf4j
@RestController
@RequestMapping("/user/")
public class UseInfoController {


    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("creatUser")
    public Rsp<UserInfoVO> creatUser(@RequestBody @Validated Req<UserInfoDTO> request) {
        UserInfoDTO dto = request.getData();
        ThreadLocalSetting.USER_INFO_THREADLOCA.set(UserEntity.saveCommon(dto.getPhoneNumber(), request.getTraceId(), dto.getPassword(), dto.getPromotionCode()));
        UserEntity userEntity = userInfoService.creatUser();
        UserInfoVO rsp = new UserInfoVO();
        BeanUtils.copyProperties(userEntity, rsp);
        return Rsp.success(rsp, request);
    }

    @PostMapping("login")
    public Rsp<UserInfoVO> login(@RequestBody Req<UserInfoDTO> request) {
        UserInfoDTO dto = request.getData();
        ThreadLocalSetting.USER_INFO_THREADLOCA.set(UserEntity.saveCommon(dto.getPhoneNumber(), request.getTraceId(), dto.getPassword(), null));
        UserEntity userEntity = userInfoService.validateUser();
        UserInfoVO rsp = new UserInfoVO();
        BeanUtils.copyProperties(userEntity, rsp);
        return Rsp.success(rsp, request);
    }

}
